﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;

namespace Invini.Tools
{
    public class BindingProxy : Control
    {
        public event PropertyChangedCallback ValueChanged;

        public static readonly DependencyProperty FromProperty = DependencyProperty.Register("From", typeof(object), typeof(BindingProxy), new PropertyMetadata(fromChanged));
        public static readonly DependencyProperty ToProperty = DependencyProperty.Register("To", typeof(object), typeof(BindingProxy));

        public object From
        {
            get { return this.GetValue(FromProperty); }
            set { this.SetValue(FromProperty, value); }
        }
        public object To
        {
            get { return this.GetValue(ToProperty); }
            set { this.SetValue(ToProperty, value); }
        }
        public BindingProxy()
        {
            this.IsTabStop = false;
            this.Focusable = false;
            this.DataContextChanged += this.dataContextChanged;
        }

        private void dataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            this.To = null;
            this.To = this.From;
        }
        protected override Size MeasureOverride(Size constraint)
        {
            return new Size(0, 0);
        }
        protected override Size ArrangeOverride(Size arrangeBounds)
        {
            return new Size(0, 0);
        }
        protected override void OnRender(DrawingContext drawingContext)
        {
        }

        private static void fromChanged(DependencyObject instance, DependencyPropertyChangedEventArgs e)
        {
            BindingProxy proxy = instance as BindingProxy;
            proxy.To = e.NewValue;

            if (proxy.ValueChanged != null)
                proxy.ValueChanged(instance, e);
        }
    }
}
